﻿@inject IDataService<Material> mats

<SelectTable TItem="Material"
    Value="@SelectedMaterial"
    ValueChanged="@SelectedMaterialChanged"
    OnQueryAsync="@(Query)"
    GetTextCallback="@(x=> x.MaterialName)"
    DisplayText="面料">

    <TableColumns>
        <TableColumn @bind-Field="@context.Image" Text="图片">
            <Template Context="v">
                <ImageViewer Url="@v.Row.Image.Url"></ImageViewer>
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.MaterialName" Text="名称"></TableColumn>
        <TableColumn @bind-Field="@context.MaterialCode" Text="编码"></TableColumn>
    </TableColumns>

    <Template>
        <div class="row">
            <div class="col-2">
                <img class="img-fluid" style="max-width:60px;max-height:60px;" src="@context.Image.Url"/>
            </div>
            <div class="col-10">
                <span class="fw-bold">@context.MaterialName</span>
                <span class="d-block">@context.MaterialCode</span>
            </div>
        </div>
    </Template>

</SelectTable>

@code {
    [Parameter]
    public Material? SelectedMaterial { get; set; }

    [Parameter]
    public EventCallback<Material?> SelectedMaterialChanged{ get; set; }

    [Parameter]
    public MaterialType TypeFilter { get; set; }


    private async Task<QueryData<Material>> Query(QueryPageOptions option)
    {
        var items = await mats.QueryAsync(option);

        items.Items = items.Items?.Where(x => x.MaterialType == TypeFilter);
        items.TotalCount = items.Items == null ? 0:items.Items.Count();

        return items;
    }
}
